package com.example.bootDemo.controller;

import com.example.bootDemo.dto.UserDto;
import com.example.bootDemo.quartz.QuartzJobManager;
import com.example.bootDemo.quartz.TestQuartz;
import com.example.bootDemo.service.imp.UserService;
import com.example.bootDemo.util.RedisUtil;
import com.example.bootDemo.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * 创建查询用户web类
 *
 * @author zys20
 */
@RestController
@Api(value = "UserController", tags = "用户信息控制类")
public class UserController {

    private static final String BASE_URL = "/user";

    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private QuartzJobManager quartzJobManager;

    /**
     * @param dto
     * @Description 根据条件查询用户信息列表
     */
    @PostMapping(value = BASE_URL + "/getUserList")
    @ApiOperation(value = "根据条件查询用户信息列表", notes = "根据条件查询用户信息列表", httpMethod = "POST")
    public List<User> getUserList(UserDto dto) {
        return userService.list(dto);
    }

    /**
     * @param id
     * @Description 根据条件查询用户信息
     */
    @GetMapping(value = BASE_URL + "/getUser/{id}")
    @ApiOperation(value = "根据条件查询用户信息", notes = "根据条件查询用户信息", httpMethod = "GET")
    public List<User> getUserList(@PathVariable int id) {
        UserDto dto = new UserDto();
        dto.setId(id);
        return userService.list(dto);
    }

    /**
     * @Description 查询redis数据
     */
    @GetMapping(value = BASE_URL + "/getRedis")
    @ApiOperation(value = "查询redis数据", notes = "查询redis数据", httpMethod = "GET")
    public String getRedisInfo() {
        redisUtil.set("key", "getRedisInfo");
        return redisUtil.get("key");
    }

    /**
     * @Description 定时任务
     */
    @GetMapping("/task")
    @ApiOperation(value = "定时任务", notes = "定时任务", httpMethod = "GET")
    public void task() throws Exception {
        Random random = new Random();
        String name = String.valueOf(random.nextLong());
        quartzJobManager.addJob(TestQuartz.class, name, name, "*/1 * * * * ?", new HashMap<>());
    }
}
